package com.example.algorithm.no0414.solution;

import com.example.algorithm.no0414.Solution;

import java.util.TreeSet;

/**
 * @Description 解答-第三大的数-TreeSet
 * @ClassName SolutionImpl
 * @Author Administrator
 * @Date 2020/5/20 10:26
 * @Version 1.0.0
 */
public class TreeSetSolutionImpl implements Solution {
    @Override
    public int thirdMax(int[] nums) {
        if(nums==null || nums.length==0){
            throw new RuntimeException("error");
        }

        TreeSet<Integer> set = new TreeSet<>();
        for (Integer num: nums) {
            set.add(num);
            if(set.size() > 3){
                set.remove(set.first());
            }
        }

        return set.size()<3 ? set.last():set.first();
    }
}
